//
//  ZyaFeedOrderToolBar.swift
//  SlaughterClient
//
//  Created by Zhouweibin on 2018/1/27.
//  Copyright © 2018年 zwb. All rights reserved.
//

import UIKit

protocol ZyaFeedOrderToolBarDelegate {
    func selectProduct()
    func sumbitOrder()
}

class ZyaFeedOrderToolBar: UIView {

    var delegate: ZyaFeedOrderToolBarDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
//        let stackView = UIStackView(frame: frame)
//        stackView.distribution = .fill
//        stackView.axis = .horizontal
//        stackView.addSubview(selectedButton)
//        stackView.addSubview(submitButton)
//
        addSubview(selectedButton)
        addSubview(submitButton)
        addSubview(line)
        
        selectedButton.snp.makeConstraints { (maker) in
            maker.left.top.bottom.equalToSuperview()
            maker.width.equalTo(ScreenWidth/2)
        }
        
        line.snp.makeConstraints { (maker) in
            maker.centerX.equalToSuperview()
            maker.top.equalTo(4)
            maker.bottom.equalTo(-4)
            maker.width.equalTo(1)
        }
        
        submitButton.snp.makeConstraints { (maker) in
            maker.right.top.bottom.equalToSuperview()
            maker.left.equalTo(selectedButton.snp.right)
        }
        
        selectedButton.addTarget(self, action: #selector(selectedAction), for: .touchUpInside)
        submitButton.addTarget(self, action: #selector(submitAction), for: .touchUpInside)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc private func selectedAction() {
        if let del = delegate {
            del.selectProduct()
        }
    }
    
    @objc private func submitAction() {
        if let del = delegate {
            del.sumbitOrder()
        }
    }
    
    lazy var selectedButton: ZyaAWButton = {
        let button = ZyaAWButton(title: "选择商品", image: #imageLiteral(resourceName: "shopping"))
        button.label.textAlignment = .center
        button.backgroundColor = Theme.primaryLightColor
        return button
    } ()
    
    lazy var submitButton: ZyaAWButton = {
        let button = ZyaAWButton(title: "提交订单", image: #imageLiteral(resourceName: "shopping"))
        button.label.textAlignment = .center
        button.backgroundColor = Theme.primaryLightColor
        return button
    } ()
    
    lazy var line: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(red: 200/255, green: 200/255, blue: 200/255, alpha: 0.6)
        return view
    } ()
}

class ZyaAWButton: UIButton {
    
    var title: String?
    var image: UIImage?
    
    lazy var label: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 12)
        label.textColor = UIColor.white
        return label
    } ()
    
    lazy var imageV: UIImageView = {
        let image = UIImageView()
        return image
    } ()
    
    convenience init(title: String, image: UIImage) {
        self.init(frame: CGRect.zero)
        self.title = title
        self.image = image
        
        addSubview(label)
        addSubview(imageV)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        if let icon = image {
            imageV.image = icon
        }
        label.text = title
        
        imageV.snp.makeConstraints({ (maker) in
            maker.centerX.equalToSuperview()
            maker.top.equalTo(8)
            maker.size.equalTo(CGSize(width: 25, height: 25))
        })
        
        label.snp.makeConstraints({ (maker) in
            maker.right.left.bottom.equalToSuperview()
            maker.top.equalTo(imageV.snp.bottom).offset(4)
        })
    }
}

